package com.loong.musicdemo.ui.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.loong.musicdemo.R;
import com.loong.musicdemo.model.MV;
import com.loong.musicdemo.ui.mine.recent.PlayMvActivity;
import com.loong.musicdemo.util.FormatTimeUtil;

import java.util.List;

public class MineMvAdapter extends RecyclerView.Adapter<MineMvAdapter.ViewHolder> {
    private final RequestOptions requestOptions;
    private List<MV> mvList;
    private Context context;

    public MineMvAdapter(List<MV> mvList, Context context) {
        this.mvList = mvList;
        this.context = context;
        requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.temp);
        requestOptions.circleCropTransform();
        requestOptions.bitmapTransform( new RoundedCorners(10));
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recent_mv_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MV mv = mvList.get(position);
        holder.mvView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getAdapterPosition();
                MV mv1 = mvList.get(pos);
                Intent mvIntent = new Intent(context, PlayMvActivity.class);
                mvIntent.putExtra("mvUrl",mv1.getMvUrl());
                mvIntent.putExtra("mvId",mv1.getMvID());
                mvIntent.putExtra("mvName",mv1.getMvName());
                mvIntent.putExtra("mvPlayCount",mv1.getPlayCount());
                context.startActivity(mvIntent);
            }
        });
        Glide.with(context).load(mv.getCoverImg())
                .apply(requestOptions)
                .diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(holder.mv_img);
        holder.mv_name.setText(mv.getMvName());
        holder.mv_info.setText(FormatTimeUtil.formatTimeToStr(Integer.parseInt(mv.getDuration()))+" " +mv.getArtistName());
    }

    @Override
    public int getItemCount() {
        return mvList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View mvView;
        ImageView mv_img;
        TextView mv_name,mv_info;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mvView = itemView;
            mv_img = itemView.findViewById(R.id.mv_img);
            mv_name = itemView.findViewById(R.id.mv_name);
            mv_info = itemView.findViewById(R.id.mv_info);
        }
    }
}
